package com.mgetool.ui;

import java.util.Vector;

import org.eclipse.swt.graphics.GC;

import com.mgetool.ui.drawable.IDrawableListener;
import com.mgetool.ui.drawer.BitmapDrawerParam;
import com.mgetool.ui.drawer.DrawerInterface;



/**
 * 低级界面下的可绘制组件
 * @author wangbin
 *
 */
public abstract class AbstractDrawable implements DrawableInterface{

	protected XRect rect;
	
	protected Vector<IDrawableListener> listeners/* = new Vector<IDrawableListener>()*/;
	
	private boolean isShow = true;
	
	protected DrawerInterface drawer;
	
	public XRect getRect(){
		return rect;
	}
	
	@Override
	public int getWidth(){
		int w = 0;
		if(rect != null){
			w = rect.getWidth();
		}
		return w;
	}
	
	@Override
	public int getHeight(){
		int h = 0;
		if(rect != null){
			h = rect.getHeight();
		}
		return h;
	}
	
	@Override
	public int getX(){
		int x = 0;
		if(rect != null){
			x = rect.getX();
		}
		return x;
	}
	
	@Override
	public int getY(){
		int y = 0;
		if(rect != null){
			y = rect.getY();
		}
		return y;
	}

	@Override
	public void setPosition(int x, int y){
		if(rect == null){
			rect = new XRect(x, y, 0, 0);
		}else{
			rect.setPosition(x, y);
		}
	}
	

	@Override
	public void setBound(int x, int y, int width, int height){
		if(rect == null){
			rect = new XRect(x, y, width, height);
		}else{
			rect.setPosition(x, y);
			rect.setSize(width, height);
		}
	}

	@Override
	public boolean isClicked(int x, int y) {
		// TODO Auto-generated method stub
		if(rect != null){
//			return !(x < rect.getX() || x > rect.getX() + rect.getWidth() 
//					|| y < rect.getY() || y > rect.getY() + rect.getHeight());
			return rect.isContains(x, y);
		}
		return false;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
	}

	@Override
	public void setSize(int width, int height) {
		// TODO Auto-generated method stub
		if(rect == null){
			rect = new XRect(0, 0, width, height);
		}else{
			rect.setPosition(width, height);
		}
	}

	@Override
	public void onDraw(GC gc, int x, int y) {
		// TODO Auto-generated method stub
		setPosition(x, y);
		onDraw(gc);
	}

	@Override
	public void setListener(IDrawableListener listener) {
		// TODO Auto-generated method stub
		if(listeners == null){
			listeners = new Vector<IDrawableListener>();
		}
		if(!listeners.contains(listener)){
			listeners.addElement(listener);
		}
	}
	
	/**
	 * 通知触摸事件
	 */
	protected void noticeOnTouch(){
		if(listeners != null){
			for(int i = 0; i < listeners.size(); i++){
				listeners.elementAt(i).drawableOnTouch(this);
			}
		}
	}

	@Override
	public boolean isShow() {
		// TODO Auto-generated method stub
		return isShow;
	}

	@Override
	public void setShow(boolean isShow) {
		// TODO Auto-generated method stub
		this.isShow = isShow;
	}

	@Override
	public DrawerInterface getDrawer() {
		// TODO Auto-generated method stub
		return drawer;
	}

	@Override
	public void setDrawer(DrawerInterface drawer) {
		// TODO Auto-generated method stub
		this.drawer = drawer;
	}

	@Override
	public void setDrawerParam(BitmapDrawerParam drawerParam) {
		// TODO Auto-generated method stub
//		if(getDrawer() == null){
//			setDrawer(new BitmapDrawer(null));
//		}
//		getDrawer().setDrawerParam(drawerParam);
	}
	
}
